package com.luchao;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 *
 * @author LuChao
 * @since 1.0.0
 */
public class YouneedGenerateUtil {
    public static void Generate() throws Exception {
        String url = "http://www.youneed.win/free-ss";
        String rawHTML = null;
        try {
            rawHTML = getHTMLContent(url);
        } catch (IOException e) {
            System.out.println("该站异常：" + url);
            return;
        }
        Document doc = null;
        try{
            if (null == rawHTML || rawHTML.indexOf("</") > 0 || rawHTML.contains("Not Found")  || rawHTML.contains("404")) {
                System.out.println("该站异常：" + url);
                return;
            }
            //将当前页面转换成Jsoup的Document对象
            doc = Jsoup.parse(rawHTML);
        }catch (Exception e){
            System.out.println("该站异常：" + url);
        }

        //集合
        Elements infos = doc.select("td[align=center]");

        //账号密码集合
        List list = new ArrayList();
        Ssinfo s = null;
        if(infos.size()>10){
            for (int i = 0; i < infos.size(); i++) {
    //            Element element = infos.get(i);
    //            System.out.println("-" + element.text());
                if (i == 0 || i % 6 == 0) {
                    s = new Ssinfo();
                }
                s.setIp(infos.get(i + 0).text());
                s.setPort(infos.get(i + 1).text());
                s.setPassword(infos.get(i + 2).text());
                s.setEnctype(infos.get(i + 3).text());
                s.setUpdatedate(infos.get(i + 4).text());
                s.setCountry(infos.get(i + 5).text());
                list.add(s);
                i = i + 5;
            }

//          System.out.println("--" + list.size());
            //导出txt
            String destPath = "C:\\qcode\\youneed\\";
            String expoststr = "";
            String s2 = "";
            for (int j = 0; j < list.size(); j++) {
                Ssinfo ss = (Ssinfo) list.get(j);
                String str = ss.getEnctype() + ":" + ss.getPassword() + "@" + ss.getIp() + ":" + ss.getPort();
    //            System.out.println("拼接后：" + str);
                String s1 = "ss://" + Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
    //            System.out.println("加密后：" + s1);
                //生成二维码
                String filename = ss.getCountry() + "," + ss.getIp() + "," + ss.getPort() + "," + ss.getEnctype();
                QRCodeUtil.encode(s1, null, filename, destPath, true);
                expoststr += ss.getCountry() + ":" + ss.getEnctype() + ":" + ss.getPassword() + "@" + ss.getIp() + ":" + ss.getPort() + ":" + ss.getUpdatedate() + "\r\n";
                s2 += s1 + "\r\n";
            }
            //导出txt
            OutputStream fos = new FileOutputStream(destPath + "youneed_ssr1.txt");
            fos.write(expoststr.getBytes("UTF-8"));    //将字符串变成字节byte数组，使用UTF-8编码
            fos.close();

            //导出txt
            OutputStream fos1 = new FileOutputStream(destPath + "youneed_ssr2.txt");
            fos1.write(s2.getBytes("UTF-8"));    //将字符串变成字节byte数组，使用UTF-8编码
            fos1.close();
        }

    }

    //根据url地址获取对应页面的HTML内容，我们将上一节中的内容打包成了一个方法，方便调用
    private static String getHTMLContent(String url) throws IOException {

        //建立一个新的请求客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //使用HttpGet方式请求网址
        HttpGet httpGet = new HttpGet(url);
        //获取网址的返回结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取返回结果中的实体
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity);
        //关闭HttpEntity流
        EntityUtils.consume(entity);
        return content;

    }
}
